package sm.dam.api;

import org.apache.commons.lang.RandomStringUtils;

import sm.dam.MssqlDDLAdaptor;
import sm.dam.OracleDDLAdaptor;

public class DDLAdaptorFactory {

	public static final DDLAdaptor ORACLE = new OracleDDLAdaptor();

	public static final DDLAdaptor MSSQL = new MssqlDDLAdaptor();

	public static DDLAdaptor getDDLAdaptor(String type) {
		if (type.startsWith("oracle")) {
			return ORACLE;
		} else if (type.startsWith("sqlserver")) {
			return MSSQL;
		} else {
			return null;
		}
	}

	public static String createRelatedEntity(String entityName, int length,
			String prefix, String suffix) {
		int endIdx = entityName.length();
		endIdx = endIdx > length ? length : endIdx;
		String relatedEntity = entityName.substring(0, endIdx);
		if (prefix != null) {
			relatedEntity = prefix + relatedEntity;
		}
		if (suffix != null) {
			relatedEntity = relatedEntity + suffix;
		}
		return relatedEntity;
	}

	public static String createIndexName(String tableName) {
		String suffix = RandomStringUtils.randomNumeric(5);
		return createRelatedEntity(tableName, 20, null, suffix);
	}

	public static String createTempEntityName(String entityName) {
		return createRelatedEntity(entityName, 20, "TEMP_", null);
	}
}
